package com.example.panpan.controller;

import com.example.panpan.Service.GoodsService;
import com.example.panpan.domain.GoodsEntity;
import com.example.panpan.domain.GoodsResponse;
import com.example.panpan.domain.PageResult;
import com.example.panpan.domain.utils.ResponseWrapper;
import com.example.panpan.picture.PictureService;
import com.example.panpan.utilsse.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GoodsController {
    @Resource(name = "goodsServiceImpl")
    private GoodsService goodsService;

    @Autowired
    private PictureService pictureService;

    //@GetMapping("/public/interests")
    @GetMapping("/public/goods")
    public ResponseWrapper<List<GoodsResponse>> goodsGet(@RequestParam(value = "title", required = false) String title) {
        return new ResponseWrapper<>(goodsService.getGoods(title));
    }

    //@GetMapping("/public/interests/interest")
    @GetMapping("/public/goods/good")
    public ResponseWrapper<GoodsEntity> goodsGetById(@RequestParam(value = "id") int id) {
        return new ResponseWrapper<>(goodsService.getGoodsById(id));
    }

    //@PostMapping("/admin/interest/upload/picture")
    @PostMapping("/admin/good/upload/picture")
    public ResponseWrapper<Map<String, String>> uploadPicture(@RequestParam("picture") MultipartFile picture) {

        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("time", DateUtil.currentTimestamp());

        String pictureUrl = pictureService.saveImage(picture);
        resultMap.put("url", pictureUrl);
        return new ResponseWrapper<>(resultMap);
    }

    //@PostMapping("/admin/interests/interest")
    @PostMapping("/admin/goods/good")
    public ResponseWrapper<GoodsEntity> goodsInsert(@RequestBody GoodsEntity goodsEntity) {

        goodsService.insertEntity(goodsEntity);

        return new ResponseWrapper<>(goodsEntity);
    }

    //@PutMapping("/admin/interests/interest")
    @PutMapping("/admin/goods/good")
    public ResponseWrapper<GoodsEntity> goodsUpdate(@RequestBody GoodsEntity goodsEntity) {

        goodsService.updateEntity(goodsEntity);

        return new ResponseWrapper<>(goodsEntity);
    }

    //@GetMapping("/admin/interests")
    @GetMapping("/admin/goods")
    public ResponseWrapper<PageResult> goodsList(@RequestParam(value = "cid", required = false) String cid,@RequestParam("pageSize") int pageSize, @RequestParam("page") int page) {
        PageResult pageResult = new PageResult();
        pageResult.setData(goodsService.goodsList(cid, pageSize, page * pageSize));
        pageResult.setTotalCount(goodsService.goodsSize(cid, pageSize, page * pageSize));
        return new ResponseWrapper<>(pageResult);
    }

    @DeleteMapping("/admin/goods")
    public ResponseWrapper<List<String>> deleteGoods(@RequestBody List<String> groupId) {
        goodsService.deleteGoods(groupId);
        return new ResponseWrapper<>(groupId);
    }

   /* @GetMapping("/public/banners")
    public ResponseWrapper<List<BannerResponse>> bannersGet() {
        return new ResponseWrapper<>(interestService.getBanners());
    }

    @PutMapping("/admin/banners/set")
    public ResponseWrapper<List<String>> bannersSet(@RequestBody List<String> groupId) {
        interestService.updateBanners(groupId, 1);
        return new ResponseWrapper<>(groupId);
    }

    @PutMapping("/admin/banners/del")
    public ResponseWrapper<List<String>> bannersDel(@RequestBody List<String> groupId) {
        interestService.updateBanners(groupId, 0);
        return new ResponseWrapper<>(groupId);
    }*/

}
